package org.gatorojo.servlet;

import java.io.IOException;
//import java.util.Iterator;
//import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.gatorojo.enumerations.Actions;
import org.gatorojo.exception.GatoRojoException;
import org.gatorojo.model.Product;
//import org.gatorojo.model.Product;
import org.gatorojo.dao.ProductService;

public class GatoRojo extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private String contextPath;

    public void doGet (HttpServletRequest request,
                       HttpServletResponse response)
    throws ServletException, IOException
    {
    	
//    	Formas de iterar
//    	List<Product> products=null;
//		try {
//			products = service.listProduct();
//		} catch (GatoRojoException e1) {
//			e1.printStackTrace();
//		}
//    	**********************************
//    	1)
//    	int tam = products.size();
//    	for(int i=0; i<tam; i++){
//    		products.get(i).getName();
//    	}
    	
//    	**********************************
//    	2)
//    	Iterator<Product>iter = products.iterator();
//		
//    	while(iter.hasNext()){
//    		iter.next().getName();
//    	}
    	
//    	**********************************
//    	3)
//    	for(Product product:products){
//    		product.getName();
//    	}
    	
    	Actions action = Actions.valueOf(request.getParameter("action"));
    	
    	ProductService service = new ProductService();
    	
    	HttpSession session = (HttpSession)request.getSession();
    	RequestDispatcher dispatcher = null;
    	contextPath = request.getContextPath();
    	
    	// Variables para contener los datos enviados en forma de parametros.
    	int code = 0;
    	String name = "";
    	double stock = 0.0;
    	
    	// Variable para contener en forma de producto los datos enviados como parametros.
    	Product product = null;
    	
    	switch(action) {
    	case LIST:    		
    		try {
    			session.setAttribute("products", service.listProduct());
    			response.sendRedirect(contextPath +"/view/jsp/listProduct.jsp");
    		} catch(GatoRojoException e) {
    			response.sendRedirect(contextPath + "/view/error.html");
    		}
    		break;
    	case SHOW_ADD:  
    		dispatcher = getServletContext().getRequestDispatcher("/view/jsp/addProduct.jsp");
    		dispatcher.forward(request, response);
    		break;
    	case ADD:
    		code = Integer.parseInt((String)request.getParameter("productCode"));
    		name = (String)request.getParameter("productName");
    		stock = Double.parseDouble((String)request.getParameter("productStock"));
    		product = new Product(code,name,stock);
    		Product product2 = new Product();
    		product2 = service.readProduct(code);
    		if(product2==null)
    		{
    			service.readProduct(code);    		    	
        		session.setAttribute("message", "Product added successfully!");
        		response.sendRedirect(contextPath +"/GatoRojo?action=LIST"); 
    		}
    		else
    		{
    			session.setAttribute("message", "Codigo ingresado ya existe!");
    			response.sendRedirect(contextPath +"/GatoRojo?action=SHOW_ADD");  
    		}
    		break;
    	case CANCEL_ADD:
    		session.setAttribute("message", "Add product canceled!");
    		response.sendRedirect(contextPath +"/GatoRojo?action=LIST");
    		break;
    	case SHOW_DELETE:
    		dispatcher = getServletContext().getRequestDispatcher("/view/jsp/deleteProduct.jsp");
    		dispatcher.forward(request, response);
    		break;
    	case DELETE:
    		code = Integer.parseInt((String)request.getParameter("productCode"));
    		service.deleteProduct(code);
    		session.setAttribute("message", "Product deleted successfully!");    		
    		response.sendRedirect(contextPath +"/GatoRojo?action=LIST");
    		break;
    	case CANCEL_DELETE:
    		session.setAttribute("message", "Delete product canceled!");
    		response.sendRedirect(contextPath +"/GatoRojo?action=LIST");
    		break;
    	case SHOW_UPDATE: 
    		product = service.readProduct(Integer.parseInt((String)request.getParameter("productCode")));
    		request.setAttribute("productName", product.getName());
    		request.setAttribute("productStock", product.getStock());
    		dispatcher = getServletContext().getRequestDispatcher("/view/jsp/updateProduct.jsp");
    		dispatcher.forward(request, response);
    		break;
    	case UPDATE:
    		code = Integer.parseInt((String)request.getParameter("productCode"));
    		name = (String)request.getParameter("productName");
    		stock = Double.parseDouble((String)request.getParameter("productStock"));
    		product = new Product(code,name,stock);
    		service.updateProduct(product);    		    	
    		session.setAttribute("message", "Product updated successfully!");
    		response.sendRedirect(contextPath +"/GatoRojo?action=LIST");
    		break;
    	case CANCEL_UPDATE:
    		session.setAttribute("message", "Update product canceled!");
    		response.sendRedirect(contextPath +"/GatoRojo?action=LIST");
    		break;
    	default:
    			response.sendRedirect(contextPath +"/view/error.html");    		
    		break;
    	}    	    
    }
    
    public void doPost (HttpServletRequest request,
                       HttpServletResponse response) throws ServletException, IOException {    
    }

    public void init() throws ServletException {
    }

    public void destroy() {
    }
}